package org.dromara.kitchen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.kitchen.domain.BdCity;
import org.dromara.kitchen.domain.BdDistrict;
import org.dromara.kitchen.domain.BdProvince;
import org.dromara.kitchen.domain.dto.AreaTreeSelect;
import org.dromara.kitchen.service.IBdCityService;
import org.dromara.kitchen.service.IBdDistrictService;
import org.dromara.kitchen.service.IBdProvinceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 全国的 省份Controller
 *
 * @author mr.zhou
 * @date 2021-03-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/area")
public class AreaController extends BaseController {

    private final IBdProvinceService provinceService;
    private final IBdCityService cityService;
    private final IBdDistrictService districtService;


    /**
     * 查询全国的 省份列表
     */

    @GetMapping("/listProv")
    public TableDataInfo<BdProvince> listProv(BdProvince province) {
        PageUtils.buildPage();
        QueryWrapper<BdProvince> qw = new QueryWrapper<>();
        if(province.getProvinceName() != null){
            qw.like("province_name",province.getProvinceName());
        }
        return PageUtils.buildDataInfo(provinceService.page(PageUtils.buildPage(),qw));
    }

    /**
     * 查询全国的 省份列表
     */
    @GetMapping("/listProvNoPage")
    public R listProvNoPage() {
        List<BdProvince> list = provinceService.list();
        return R.ok(list);
    }


    /**
     * 查询全国的 省份列表
     */
    @GetMapping("/getAllArea")
    public R getAllArea(BdProvince province) {
        List<AreaTreeSelect> list = provinceService.getAllArea(province);
        return R.ok(list);
    }

    /**
     *  根据省份id 查询城市列表
     */
    @GetMapping("/listCityPage")
    public TableDataInfo<BdCity> listCityPage(BdCity emCity) {
        PageUtils.buildPage();
        QueryWrapper<BdCity> qw = new QueryWrapper<>();
        qw.eq("province_id",emCity.getProvinceId());
        return PageUtils.buildDataInfo(cityService.page(PageUtils.buildPage(),qw));
    }

    /**
     *  根据省份id 查询城市列表
     */
    @GetMapping("/listCity")
    public R listCity(BdCity emCity) {
        QueryWrapper<BdCity> qw = new QueryWrapper<>();
        qw.eq("province_id",emCity.getProvinceId());
        List<BdCity> list = cityService.list(qw);
        return R.ok(list);
    }

    /**
     * 根据城市id 查询区县列表
     */
    @GetMapping("/listDistrictPage")
    public TableDataInfo<BdDistrict> listDistrictPage(BdDistrict district) {
        PageUtils.buildPage();
        QueryWrapper<BdDistrict> qw = new QueryWrapper<>();
        qw.eq("city_id",district.getCityId());
        return PageUtils.buildDataInfo(districtService.page(PageUtils.buildPage(),qw));
    }

    /**
     * 根据城市id 查询区县列表
     */
    @GetMapping("/listDistrict")
    public R listDistrict(BdDistrict district)
    {
        QueryWrapper<BdDistrict> qw = new QueryWrapper<>();
        qw.eq("city_id",district.getCityId());
        List<BdDistrict> list = districtService.list(qw);
        return R.ok(list);
    }
}
